/* typehints:start */
import { GameRoot } from "../root";
/* typehints:end */

import { HUDWorldPage } from "./parts/world_page";
import { HUDBottomBar } from "./parts/bottom_bar";
import { HUDShowMessage } from "./parts/show_message";
import { Signal } from "../../core/utils/signal";
import { HUDNpcBoard } from "./parts/npc_board";
import { HUDTaskPage } from "./parts/task_page";
import { HUDGoodsBoard } from "./parts/goods_board";
import { HUDBackpickPage } from "./parts/backpack_page";
import { HUDPlayerPage } from "./parts/player_page";
import { HUDLogsBoard } from "./parts/logs";
import { HUDBattlePage } from "./parts/battle_page";
import { HUDDialogue } from "./parts/dialogue";
import { HUDShopBoard } from "./parts/shop_board";

export class GameHUD{
  /**
   * 
   * @param {GameRoot} root 
   */
  constructor(root){
    /** @type {GameRoot} */
    this.root = root;
  }

  initialize(){
    this.signals = {
      // 发送信息的signal
      showMessage: new Signal,
      // 结束对话
      closeDialogue: new Signal,

      // 界面尺寸发生变化的signal
      resized: new Signal
    }

    this.parts = {
      // 游戏页面内的不同页面
      worldPage: new HUDWorldPage(this.root),
      backpackPage: new HUDBackpickPage(this.root),
      taskPage: new HUDTaskPage(this.root),
      playerPage: new HUDPlayerPage(this.root),
      battlePage: new HUDBattlePage(this.root),

      // 页面中的组件
      bottomBar: new HUDBottomBar(this.root),
      showMessage: new HUDShowMessage(this.root),
      npcBoard: new HUDNpcBoard(this.root),
      goodsBoard: new HUDGoodsBoard(this.root),
      shopBoard: new HUDShopBoard(this.root),
      logBoard: new HUDLogsBoard(this.root),
      dialogue: new HUDDialogue(this.root)
    }

    const frag = document.createDocumentFragment();
    for(const key in this.parts){
      this.parts[key].createElements(frag);
    }

    document.body.appendChild(frag);

    for(const key in this.parts){
      this.parts[key].initialize();
    }
  }

  update(){
    if(!this.root.gameInitialized){
      return;
    }

    for(const key in this.parts){
      this.parts[key].update();
    }
  }

  cleanup(){
    for (const key in this.parts) {
      this.parts[key].cleanup();
    }

    for (const key in this.signals) {
      this.signals[key].removeAll();
    }
  }
}